package no.ntnu.fp.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Date;

import no.ntnu.fp.swingutil.CalendarTools;

public class ActivityModel {

	public static final String STARTDATE = "startdate";
	public static final String ENDDATE = "enddate";
	public static final String LOCATION = "location";
	public static final String DESCRIPTION = "description";
	public static final String NAME = "name";
	public static final String PARTICIPANTS = "participants";
	
	private PropertyChangeSupport pcs;
	
	private ActivityType type;
	
	private int ID;
	private Date startDate, endDate;
	private Location location;
	private String description;
	private String name;
	private ArrayList<Person> participants;
	private ArrayList<AnswerState> answers;
	
	/**
	 * The standard constructor
	 * @param type
	 * The type of activity, ActivityType.MEETING or ActivityType.APPOINTMENT
	 */
	public ActivityModel(ActivityType type) {
		pcs = new PropertyChangeSupport(this);
		this.type = type;
		participants = new ArrayList<Person>();
		answers = new ArrayList<AnswerState>();
		startDate = null;
		endDate = null;
		location = new Location("");
		description = "";
		name = "";
	}
	
	/**
	 * A method for checking whether a certain time span crashes with this Activity. 
	 * @param start
	 *  The start date and time
	 * @param end
	 * The end date and time
	 * @return True it collides, or False it don't
	 */
	public boolean collides(Date start, Date end){
		CalendarTools ct = new CalendarTools();
		if ( ct.isSameDate(start, startDate, false) && ct.isSameDate(end, endDate, false) )
			return true; //exact same time
		if ( ct.isFirstDateBeforeSecondDate(startDate, start, false) && 
			ct.isFirstDateBeforeSecondDate(start, endDate, false) )
			return true; // crosses "start"
		if ( ct.isFirstDateBeforeSecondDate(startDate, end, false) &&
				ct.isFirstDateBeforeSecondDate(end, endDate, false) )
			return true; // crosses "end"
		if ( ct.isFirstDateBeforeSecondDate(start, startDate, false) &&
				ct.isFirstDateBeforeSecondDate(endDate, end, false) )
			return true; // is in between
		return false;
	}
	
	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}
	
	public ActivityType getType() {
		return type;
	}

	public void setType(ActivityType type) {
		this.type = type;
	}
	
	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(Date startDate) {
		Date oldValue = this.startDate;
		this.startDate = startDate;
		pcs.firePropertyChange(STARTDATE, oldValue, startDate);
	}
	
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		Date oldValue = this.endDate;
		this.endDate = endDate;
		pcs.firePropertyChange(ENDDATE, oldValue, endDate);
	}

	public Location getLocation() {
		return location;
	}
	
	public void setLocation(Location location) {
		Location oldValue = this.location;
		this.location = location;
		pcs.firePropertyChange(LOCATION, oldValue, location);
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		String oldValue = this.description;
		this.description = description;
		pcs.firePropertyChange(DESCRIPTION, oldValue, description);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		String oldValue = this.name;
		this.name = name;
		pcs.firePropertyChange(NAME, oldValue, name);
	}

	public ArrayList<Person> getParticipants() {
		return participants;
	}

	public void setParticipants(ArrayList<Person> participants) {
		ArrayList<Person> oldValue = this.participants;
		this.participants = participants;
		pcs.firePropertyChange(PARTICIPANTS, oldValue, participants);
	}
	
	public Person getLeader() {
		return participants.get(0);
	}
	
	public ArrayList<AnswerState> getAnswers() {
		return answers;
	}

	public void setAnswers(ArrayList<AnswerState> answers) {
		this.answers = answers;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}
	
	public String toString() {
		return getName();
	}
	
}
